#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/*./hello_test /dev/xxx */
/*./hello_test /dev/xxx string */


int main(int argc, char** argv)
{
    int fd;
    int len;
    char buf[100];

    if(argc != 2 && argc != 3)
    {
        printf("Usage:%s <dev> [string]\n", argv[0]);
        return -1;
    }

    /*open*/
    fd = open(argv[1], O_RDWR);
    if(fd < 0)
    {
        printf("open %s err\n",argv[1]);
        return -1;
    }

    if(argc == 2)
    {
        /*read*/
        len = read(fd, buf, 100);
        buf[100] = '\0';
        printf("read string: %s \n",buf);

    }

    if(argc == 3)
    {
        /*write*/
       len = write(fd, argv[2], strlen(argv[2]) + 1);
       printf("write len = %d\n", len);
    }

    close(fd);
    return 0;
}